GN config

The config syntax allows you to encapsulate compiler flags, include directories, defines, and other build settings in a reusable way.

Here's a detailed explanation of the config syntax in GN:

Defining a Config

A config is defined using the config keyword followed by the config's name. The body of the config contains the settings you want to apply.

config("my_config") {
 # Config settings go here
}

Config Settings

Inside a config, you can specify various settings such as compiler flags (cflags, cflags_cc, cflags_cxx), defines (defines), include directories (include_dirs), and more. These settings are applied to any target that includes this config.

config("my_config") {
 include_dirs = [ "src/includes" ]
 defines = [ "ENABLE_DEBUG=1" ]
 cflags = [ "-Wall", "-Wextra" ]
}

Applying a Config to a Target

To use a config in a target, add it to the target's configs list. This makes all settings in the config apply to the target.

executable("my_executable") {
 configs = [ ":my_config" ]
 ...
}

Visibility

Similar to targets, a config can have visibility rules defining which targets can reference it. This is useful for encapsulating configuration that should only be used by specific parts of the project.

config("my_config") {
 visibility = [":some_targets"]
 ...
}

Purpose

The main purposes of config in GN are:

Layering

Multiple config entities can be applied to a single target, and they are additive. If there are conflicts (like in defines), the last applied config takes precedence. This allows for sophisticated layering and overriding of settings.


本文作者:Maeiee

本文链接:GN config

版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!


喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!